1795F - Blocking Chips - CodeForces Solution


binary search constructive algorithms dfs and similar greedy trees *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int t,n,x,y,g[1<<20],k,a[1<<20],l,r,d,v;
vector<int>e[1<<20]; 
int D(int x,int f){
	int p=g[x],q=0,r;
	for(int y:e[x])
		if(y^f){
			r=D(y,x);
			if(r<0&&p)
				return v=0;
			r<0?p=-r:q=max(q,r);
		}
	if(!p)
		return q+1;
	if(p<=q+1)
		return 0;
	return -p+1;
}
int C(int x){
	for(int i=1;i<=k;i++)
		g[a[i]]=(x+k-i)/k+1;
	v=1,v&=D(1,0)>=0;
	return v;
}
main(){
	cin>>t;
	while(t--&&cin>>n){
		for(int i=1;i<=n;i++)
			e[i].clear(),g[i]=0;
		for(int i=1;i<n;i++)
			cin>>x>>y,e[x].push_back(y),e[y].push_back(x);
		cin>>k,l=0,r=n;
		for(int i=1;i<=k;i++)
			cin>>a[i];
		while(l<r)
			d=(l+r+1)/2,C(d)?l=d:r=d-1;
		cout<<l<<'\n';
	}
}


Comments

Submit
0 Comments
More Questions

510B - Fox And Two Dots
616D - Longest k-Good Segment
1604A - Era
555B - Case of Fugitive
551A - GukiZ and Contest
1399F - Yet Another Segments Subset
1371C - A Cookie for You
430B - Balls Game
1263A - Sweet Problem
1332B - Composite Coloring
254A - Cards with Numbers
215A - Bicycle Chain
1288B - Yet Another Meme Problem
1201C - Maximum Median
435A - Queue on Bus Stop
1409B - Minimum Product
723B - Text Document Analysis
1471C - Strange Birthday Party
1199A - City Day
1334A - Level Statistics
67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt